package com.gome.boot.config.gfs;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;

/**
 * GFS基础配置.
 *
 * @author baoxiufeng
 */
public abstract class BaseGfsConfig {
    private static final String KEY_GFS_API_URL = "gfs.api.url";
    private static final String KEY_GFS_API_TOKEN = "gfs.api.token";

    @Autowired
    protected Environment env;

    /**
     * 默认的GFS客户端工具.
     *
     * @return 默认的GFS客户端工具
     */
    @Bean
    public GfsClient gfsClient() {
        return createClientByDiamondKey(KEY_GFS_API_URL, KEY_GFS_API_TOKEN);
    }

    /**
     * 创建GFS客户端工具类.
     *
     * @param url GFS地址
     * @param token GFS Token
     * @return GFS客户端工具实例
     */
    protected GfsClient createClient(String url, String token) {
        return new GfsClient(url, token);
    }

    /**
     * 创建GFS客户端工具类.
     *
     * @param urlDiamondKey Diamond中的GFS地址KEY
     * @param tokenDiamondKey Diamond中的GFS Token KEY
     * @return GFS客户端工具实例
     */
    protected GfsClient createClientByDiamondKey(String urlDiamondKey, String tokenDiamondKey) {
        return new GfsClient(env.getRequiredProperty(urlDiamondKey), env.getRequiredProperty(tokenDiamondKey));
    }
}
